package com.leyou.listener;

import com.leyou.common.constants.RocketMQConstants;
import com.leyou.item.client.ItemClient;
import com.leyou.mapper.GoodsRepository;
import com.leyou.pojo.Goods;
import com.leyou.service.SearchService;
import com.netflix.discovery.converters.Auto;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: guojy
 * @date: 2020/3/14 17:11
 * @Description: 监听商品上架，向ES中添加商品数据
 * @version: 1.0
 */
@Slf4j
@Component
// 参数一：topic,参数二：tag，参数三：组名
@RocketMQMessageListener(topic = RocketMQConstants.TOPIC.ITEM_TOPIC_NAME,
        selectorExpression = RocketMQConstants.TAGS.ITEM_UP_TAGS,
        consumerGroup = "ITEM_SEARCH_UP")
public class ItemUpListener implements RocketMQListener<Long> {
    @Autowired
    private SearchService searchService;
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private GoodsRepository goodsRepository;

    // 处理逻辑信息
    @Override
    public void onMessage(Long spuId) {
        log.info("[搜索服务]- (商品上架) -接收消息，spuId={}", spuId);
        // 操作 ES进行商品添加
        // 构建  GOODS
        Goods goods = searchService.buildGoods(itemClient.findSpuById(spuId));
        // 添加 GOODS
        goodsRepository.save(goods);

    }
}
